package com.pureut.quality.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.common.core.exception.GlobalException;
import com.pureut.common.core.utils.bean.BeanUtils;
import com.pureut.quality.domain.AuditNonconformity;
import com.pureut.quality.domain.ExternalAuditPlan;
import com.pureut.quality.domain.dto.AuditNonconformityDto;
import com.pureut.quality.domain.dto.ExternalAuditPlanDto;
import com.pureut.quality.domain.vo.AuditNonconformityVo;
import com.pureut.quality.domain.vo.ExternalAuditPlanVo;
import com.pureut.quality.domain.vo.SubmitVo;
import com.pureut.quality.mapper.AuditNonconformityMapper;
import com.pureut.quality.mapper.ExternalAuditPlanMapper;
import com.pureut.quality.service.AuditNonconformityService;
import com.pureut.quality.service.ExternalAuditPlanService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/***
 * 外审不符合项报告
 * Author:C
 * Date:2023/01/19 17:15
 * @DESC
 */
@Service
public class AuditNonconformityServiceImpl extends ServiceImpl<AuditNonconformityMapper, AuditNonconformity> implements AuditNonconformityService {

    /**
     * 外审不符合项报告 数据层
     */
    @Resource
    AuditNonconformityMapper auditNonconformityMapper;

    /**
     *查询外审计划列表
     * @param auditNonconformityVo
     * @return
     */
    @Override
    public List<AuditNonconformityDto> listAuditNonconformity(AuditNonconformityVo auditNonconformityVo) {

        List<AuditNonconformityDto> list = auditNonconformityMapper.listAuditNonconformity(auditNonconformityVo);
        return list;
    }

    /**
     *查看外审计划
     * @param id
     * @return
     */
    @Override
    public AuditNonconformityDto getInfo(Long id) {
        return auditNonconformityMapper.getInfo(id);
    }

    /**
     *新增外审计划管理
     * @param auditNonconformityVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertAuditNonconformity(AuditNonconformityVo auditNonconformityVo) {
        AuditNonconformity auditNonconformity = new AuditNonconformity();
        BeanUtils.copyProperties(auditNonconformityVo, auditNonconformity);
        auditNonconformity.setStatus("1");
        return save(auditNonconformity);
    }

    /**
     * 修改外审计划管理
     * @param auditNonconformityVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateAuditNonconformity(AuditNonconformityVo auditNonconformityVo) {
        AuditNonconformity byId = getById(auditNonconformityVo.getId());

        AuditNonconformity auditNonconformity = new AuditNonconformity();
        BeanUtils.copyProperties(auditNonconformityVo, auditNonconformity);
        if (byId.getStatus().equals("4")){
            auditNonconformity.setStatus("1");
        }
        auditNonconformity.setId(auditNonconformityVo.getId());
        return updateById(auditNonconformity);
    }

    /**
     * 删除外审计划管理
     * @param ids
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeAuditNonconformity(String ids) {
        return removeById(ids);
    }

    /**
     * 提交
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean submitAuditNonconformity(Long id) {
        AuditNonconformity byId = getById(id);
        if (!byId.getStatus().equals("1")){
            throw new GlobalException("当前单据不是待提交状态无法进行提交!");
        }
        byId.setStatus("2");
        return updateById(byId);
    }

    /**
     * 审核
     * @param submitVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean examineAuditNonconformity(SubmitVo submitVo) {
        AuditNonconformity byId = getById(submitVo.getId());
        if (!byId.getStatus().equals("2")){
            throw new GlobalException("当前单据不是待审核状态无法进行审核!");
        }
        if (submitVo.getAuditState() == 1){
            byId.setStatus("3");
        } else {
            byId.setStatus("4");
        }
        return updateById(byId);
    }

    /**
     * 撤销
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean revokeAuditNonconformity(Long id) {
        AuditNonconformity byId = getById(id);
//        if (!byId.getStatus().equals("2")){
//            throw new GlobalException("当前单据不是待审核状态无法进行审核!");
//        }
        byId.setStatus("1");
        return updateById(byId);
    }

    /**
     * 关结
     * @param submitVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean closeAuditNonconformity(SubmitVo submitVo) {
        List<AuditNonconformity> auditNonconformityList = auditNonconformityMapper.selectList(new QueryWrapper<AuditNonconformity>()
                .lambda().in(AuditNonconformity::getId, submitVo.getIdList()));
        auditNonconformityList.forEach(e -> e.setStatus("5"));
        return updateBatchById(auditNonconformityList);
    }
}
